package app.cloudstringers;

import java.util.ArrayList;

import model.AllItem;
import utils.Utils;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import controller.AllTabsAsync;
import controller.GetTokenString;
import define.Constants;
import define.DefaultValue;
import define.SharedPreference;

public class All extends Activity {
	/**
	 * String section
	 */
	public static boolean RUN_TIME_IN_LAUNCH = false;
	public static boolean SAVED_IN_PREFS = false;

	/**
	 * Data section
	 */
	// public static Country country = new Country();

	public static ArrayList<String> mAlCountryNameItem = new ArrayList<String>();
	public static ArrayAdapter<String> mAaCountryAdapter = null;
	public static ArrayList<AllItem> mAlItem = new ArrayList<AllItem>();

	/**
	 * View section
	 */
	private static AlertDialog.Builder mAdLogin, mAdLogout, mAdExit;
	private static AlertDialog.Builder mAdConnection;
	public static AlertDialog.Builder mAdMarketPlace, mAdAuction;

	public static Dialog mDialogMarketPlace, mDialogAuction;
	public static Dialog mDialogInfo;

	/**
	 * Others section
	 */
	public static Context mContextAll;
	private SharedPreferences.Editor mSpEditor;
	private SharedPreferences mSpSettings;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_all);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

		mContextAll = this;

		// Initial dialog
		initialDialogs();

		// Initial dialogs
		initialAlertDialogs();

		// Get shared preferences
		mSpSettings = getSharedPreferences(SharedPreference.PREFS,
				Context.MODE_PRIVATE);
		mSpEditor = mSpSettings.edit();
	}

	/**
	 * Clicked button
	 */
	public void onClickAll(View v) {
		switch (v.getId()) {
		case R.id.ibtn_home_in_all:
			// new Intent to menu class
			Intent intent = new Intent(All.this, Home.class);
			finish();
			startActivity(intent);
			break;
		case R.id.ibtn_logout_in_all:
			mAdLogout.show();
			break;
		case R.id.ibtn_refresh_in_all:
			refreshAllListItem();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			if (mDialogMarketPlace.isShowing()) {
				mDialogMarketPlace.dismiss();
			}
			if (mDialogAuction.isShowing()) {
				mDialogAuction.dismiss();
			}
			if (mDialogInfo.isShowing()) {
				mDialogInfo.dismiss();
			}
			mAdExit.show();
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onResume() {
		super.onResume();

		if (Constants.utils.isNetworkOnline()) {
			refreshAllListItem();
		} else {
			Utils.hideConnectionDialog();
			Utils.showConnectionDialog();
		}
	}

	/**
	 * Initial methods
	 */
	private void initialAlertDialogs() {
		mAdMarketPlace = new AlertDialog.Builder(this);
		mAdMarketPlace.setTitle(getString(R.string.title_market_place));
		mAdMarketPlace.setIcon(R.drawable.ibtn_error_normal);
		mAdMarketPlace.setCancelable(false);
		mAdMarketPlace.setMessage(getString(R.string.content_market_place));
		mAdMarketPlace.setPositiveButton(R.string.ok,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
					}
				});

		mAdAuction = new AlertDialog.Builder(this);
		mAdAuction.setTitle(getString(R.string.title_auction));
		mAdAuction.setIcon(R.drawable.ibtn_error_normal);
		mAdAuction.setCancelable(false);
		mAdAuction.setMessage(getString(R.string.content_auction));
		mAdAuction.setPositiveButton(R.string.ok,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
					}
				});

		mAdConnection = new AlertDialog.Builder(this);
		mAdConnection.setTitle(getString(R.string.title_failed_connection));
		mAdConnection.setIcon(R.drawable.ibtn_error_normal);
		mAdConnection.setCancelable(false);
		mAdConnection
				.setMessage(getString(R.string.content_failed_server_connection));
		mAdConnection.setPositiveButton(R.string.try_again,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
						startActivity(new Intent(All.this, Home.class));
					}
				});
		mAdConnection.setNegativeButton(R.string.quit,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
						android.os.Process.killProcess(android.os.Process
								.myPid());
					}
				});

		mAdExit = new AlertDialog.Builder(All.this);
		mAdExit.setTitle(R.string.title_exit);
		mAdExit.setIcon(R.drawable.ic_info_normal);
		mAdExit.setCancelable(false);
		mAdExit.setMessage(R.string.content_exit);
		mAdExit.setPositiveButton(R.string.ok,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
						android.os.Process.killProcess(android.os.Process
								.myPid());
					}
				});
		mAdExit.setNegativeButton(R.string.cancel,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
					}
				});

		mAdLogin = new AlertDialog.Builder(this);
		mAdLogin.setTitle(getString(R.string.title_login));
		mAdLogin.setCancelable(false);
		mAdLogin.setMessage(getString(R.string.content_login));
		mAdLogin.setPositiveButton(R.string.ok,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
						mSpEditor.putString(
								SharedPreference.PREFS_KEY_USER_NAME, "user");
						mSpEditor.putString(
								SharedPreference.PREFS_KEY_GLOBAL_ID,
								DefaultValue.DEFAULT_GLOBAL_ID + "");
						mSpEditor.commit();

						// new Intent to login class
						Intent intent = new Intent(All.this, Login.class);
						finish();
						startActivity(intent);
					}
				});

		mAdLogout = new AlertDialog.Builder(this);
		mAdLogout.setTitle(getString(R.string.title_logout));
		mAdLogout.setCancelable(false);
		mAdLogout.setMessage(getString(R.string.content_logout));
		mAdLogout.setPositiveButton(R.string.yes,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
						mSpEditor.putString(
								SharedPreference.PREFS_KEY_USER_NAME, "user");
						mSpEditor.putString(
								SharedPreference.PREFS_KEY_GLOBAL_ID,
								DefaultValue.DEFAULT_GLOBAL_ID + "");
						mSpEditor.commit();

						// new Intent to login class
						Intent intent = new Intent(All.this, Login.class);
						finish();
						startActivity(intent);
					}
				});
		mAdLogout.setNegativeButton(R.string.no,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
					}
				});
	}

	private void initialDialogs() {
		mDialogMarketPlace = new Dialog(this);
		mDialogMarketPlace.requestWindowFeature(Window.FEATURE_NO_TITLE);
		mDialogMarketPlace.setContentView(R.layout.dialog_market_place);

		mDialogAuction = new Dialog(this);
		mDialogAuction.requestWindowFeature(Window.FEATURE_NO_TITLE);
		mDialogAuction.setContentView(R.layout.dialog_auction);

		mDialogInfo = new Dialog(this);
		mDialogInfo.requestWindowFeature(Window.FEATURE_NO_TITLE);
		mDialogInfo.setContentView(R.layout.dialog_info);
	}

	/**
	 * Basic Methods
	 */
	// Refresh list item
	public void refreshAllListItem() {
		if (Constants.utils.isNetworkOnline()) {
			new AllTabsAsync(mContextAll).execute(MyVideoTab.CURRENT_TAB);

			new GetTokenString(this, true).execute();
		}
	}
}